<?php
namespace app\admin\controller\customized;
use app\admin\controller\Base;
use app\BaseController;
use think\facade\Db;
class Friendurl extends Base{
    public function list_data(){
        $listData = Db::table('tb_friend_url')->select()->toArray(); 
        foreach($listData as $k=>$v){
            $listData[$k]['up_time'] = date("Y-m-d G:i",$v['up_time']);
        }
        $this->assign('listData',json_encode($listData));
        return $this->fetch();
    
    }

    //编辑数据
    public function form_data($id=0){
        $pt = input("post.");
        if(count($pt)>0){
            $pt['up_time'] = time();
            $bool = Db::table('tb_friend_url')->where('id', $pt['id'])->data($pt)->update();
            $this->logData('修改产品:'.$pt['name']);

            if($bool == 1){
                $this->info = ['code'=>1,'msg'=>'操作成功','data'=>$pt];
            }else{
                $this->info = ['code'=>0,'msg'=>'操作失败','data'=>$pt];
            }
            return json($this->info);
        }
        if($id!=0){
            $pageData = Db::table('tb_friend_url')->where('id', $id)->find();
        }else{
            $pageData = [
                'url'=>'',
                'name'=>'',
                'is_show'=>1,
            ];
        }
        $this->assign('pageDataJson',json_encode($pageData));
        $this->assign('pageData',$pageData);
        $this->assign('id',$id);
        return $this->fetch();
    }

    //添加数据
    public function form_add_data(){
        $pt = input("post.");
        $pt['create_time'] = time();
        $pt['up_time'] = time();
        $bool = Db::table('tb_friend_url')->insert($pt);
        $this->logData('新增产品:'.$pt['name']);
        if($bool == 1){
            $this->info = ['code'=>1,'msg'=>'操作成功','data'=>$pt];
        }else{
            $this->info = ['code'=>0,'msg'=>'操作失败','data'=>$pt];
        }
        return json($this->info);
    }

    //添加数据
    public function form_del_data(){
        $pt = input("post.");
        $bool = Db::table('tb_friend_url')->where('id',$pt['id'])->delete();
        $this->logData('删除友情链接:'.$pt['id']);
        $this->info['code']=1;
        if($bool == 1){
            $this->info = ['code'=>1,'msg'=>'操作成功','data'=>$pt];
        }else{
            $this->info = ['code'=>0,'msg'=>'操作失败','data'=>$pt];
        }
        return json($this->info);
    }
}